package com.tron.dobest.java.tronthread;

/**
 * 使用同步方法解决线程安全
 */
public class ThreadStart01 {

    public static void main(String[] args) {

        TronThread tronThread = new TronThread();
        new Thread(tronThread, "1号窗口").start();
        new Thread(tronThread, "2号窗口").start();
        new Thread(tronThread, "3号窗口").start();
        new Thread(tronThread, "4号窗口").start();
        new Thread(tronThread, "5号窗口").start();
        new Thread(tronThread, "6号窗口").start();
        new Thread(tronThread, "7号窗口").start();
        new Thread(tronThread, "8号窗口").start();

    }

    static class TronThread implements Runnable {

        private static int ticket = 100;

        @Override
        public void run() {
            while (true) {
                sell();
                if (ticket <= 0) {
                    break;
                }
            }
        }


        //不通过synchronized加锁
         void sell() {
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + ":卖票，票号：" + ticket + " ---线程id:" + Thread.currentThread().getId());
                ticket--;
                System.out.println("剩余票数："+ticket);
            }
        }
    }
}

